<?php
namespace app\admin\controller;
use app\Base;
use think\facade\Db;
class User extends Base{
	//private $request;
	//控制台首页信息
    public function index()
    {
		$where=[];
		if($this->request->post('phone'))
		{
			$where[]=array('phone','like','%'.$this->request->post('phone').'%');
		}
		 
		 
		$numsize=$this->request->post('pagesize')?$this->request->post('pagesize'):15;
		
		$total=Db::name("member")->where($where)->count();
		$f=array('list_rows'=>$numsize,'page'=>$this->request->post('page'));
		
		$list=Db::name("member")->where($where)->order('userid','desc')->paginate($f)->each(function($item,$key){
			$item['ordernum']=Db::name('order')->where("userid",$item['userid'])->count();
			$item['paynum']=Db::name('paylist')->where("userid",$item['userid'])->count();
			return $item;
		})->toArray()['data'];
		
		$this->setobj(array('total'=>$total,'datalist'=>$list));
		return $this->ok();
    }
	
	public function useradd()
	{
		$info=$this->request->post();
		if(!$info['username'] || !$info['phone'])
		{
			return $this->kerror('参数不正确');
		}
		
		$data=Db::name('member')->whereRaw("username ='".$info['username']."' or phone='".$info['phone']."'")->find();
		if($data['userid'])
		{
			return $this->kerror('用户已存在');
		}
		
		
		$info['password']=skey($info['password']);
		$info['regtime']=time();
		$userid=Db::name('member')->insert($info);
		return $this->ok();
	}
	
	public function userdel()
	{
		$userid=$this->request->post('userid');
		if(!$userid)
		{
			return $this->kerror('参数不正确');
		}
		$member=Db::name('member')->where("userid",$userid)->find();
		Db::name('member')->where("userid",$userid)->delete();
		if($member['userid'])
		{
			$arr=array('other'=>$member['phone']?$member['phone']:$member['username']);
			Db::name('order')->where("userid",$userid)->update($arr);
			Db::name('member_bill')->where("userid",$userid)->update($arr);
		}
		
		return $this->ok();
		
	}
	
	public function userinfo()
	{
		$userid=$this->request->post('userid');
		if(!$userid)
		{
			return $this->kerror('参数不正确');
		}
		$member=Db::name('member')->where("userid",$userid)->find();
		if(!$member['userid'])
		{
			return $this->kerror('用户不存在');
		}
		
		
		if($this->request->post('phone') || $this->request->post('email'))
		{
			$info=$this->request->post();
			$info['password']=$info['password']?skey($info['password']):$member['password'];
			Db::name('member')->update($info);
			return $this->ok();
		}
		else{
			$member['password']='';
			$this->setobj($member);
			return $this->ok();
		}
		
		
	}
	
	
	public function grouplist()
	{
		$list=Db::name('member_group')->order('maxscore','asc')->paginate(15)->each(function($item,$key){
			$item['score']=$item['minscore'].'-'.$item['maxscore'];
			 
			return $item;
		})->toArray()['data'];
		$this->setobj(array('datalist'=>$list));
		return $this->ok();
		
	}
	
	public function groupinfo()
	{
		$groupid=$this->request->post('groupid');
		if(!$groupid)
		{
			return $this->kerror('参数错误');
		}
		
		if($this->request->post('name'))
		{
			$info=['name'=>$this->request->post('name'),'description'=>$this->request->post('description'),'maxscore'=>$this->request->post('maxscore'),'minscore'=>$this->request->post('minscore')];
			Db::name('member_group')->where("groupid",$groupid)->update($info);
			return $this->ok();
		}
		else{
			$info=Db::name('member_group')->where("groupid",$groupid)->find();
			$this->setobj($info);
			return $this->ok();
		}
	}
	
	public function groupdel()
	{
		$groupid=$this->request->post('groupid');
		if(!$groupid)
		{
			return $this->kerror('参数错误');
		}
		
		Db::name('member_group')->where("groupid",$groupid)->delete();
		return $this->ok();
	}
	
	public function groupadd()
	{
		$info=['name'=>$this->request->post('name'),'description'=>$this->request->post('description'),'maxscore'=>$this->request->post('maxscore'),'minscore'=>$this->request->post('minscore')];
		if(!$info['name'])
		{
			return $this->kerror('参数错误');
		}
		
		$groupid=Db::name('member_group')->insert($info);
		$this->setobj($groupid);
		return $this->ok();
		
	}
	
	
	 
	
}
